/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import entities.Account;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@SessionScoped
public class manageAccount {

    @EJB
    private bean.business.accountBean accountBean;
    private List<Account> list = new LinkedList<Account>();
    private Account acc = new Account();
    private Account acc1 = new Account();

    public Account getAcc1() {
        return acc1;
    }

    public void setAcc1(Account acc1) {
        this.acc1 = acc1;
    }

    public Account getAcc() {
        return acc;
    }

    public void setAcc(Account acc) {
        this.acc = acc;
    }

    public bean.business.accountBean getAccountBean() {
        return accountBean;
    }

    public void setAccountBean(bean.business.accountBean accountBean) {
        this.accountBean = accountBean;
    }

    public List<Account> getList() {
        list = accountBean.allAccount();
        return list;
    }

    public void setList(List<Account> list) {
        this.list = list;
    }

    /** Creates a new instance of accountBean */
    public manageAccount() {
    }

    public String login() throws IOException {

        acc1 = accountBean.login(acc.getAccName(),acc.getAccPass());
        if (acc1 == null ) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Username Or Password incorrect !"));
            return "login";
        } else {
            if (acc1.getUserType() == 1) {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("admin", acc.getAccName());
                FacesContext.getCurrentInstance().getExternalContext().redirect("faces/admin/home.xhtml");
            } else if (acc1.getUserType() == 0) {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("academic", acc.getAccName());
                FacesContext.getCurrentInstance().getExternalContext().redirect("faces/academic/home.xhtml");
            }

        }
        return "login";
    }

    public void insert() {
        if (checkName()) {
            accountBean.addAccount(acc1);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
        }
    }

    public void update() {
        accountBean.updateAccount(acc);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
    }

    public void delete() {
        accountBean.delAccount(acc.getAccID());
    }

    public boolean checkName() {
        List<Account> accounts = new LinkedList<Account>();
        accounts = accountBean.allAccount();
        for (Account account : accounts) {
            if (account.getAccName().equals(acc1.getAccName())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Name had been Already !"));
                return false;
            }
        }
        return true;
    }

    public void checkAdmin() throws IOException {
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("admin") == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NOT logged in !"));
            FacesContext.getCurrentInstance().getExternalContext().redirect("../login.xhtml");
            FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        }
    }

    public void checkAcademic() throws IOException {
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("academic") == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("NOT logged in !"));
            FacesContext.getCurrentInstance().getExternalContext().redirect("../login.xhtml");
        }
    }

    public String logout() throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        session.invalidate();
        FacesContext.getCurrentInstance().getExternalContext().redirect("../index.xhtml");
        return "../login";
    }
}
